var gulp = require("gulp");
var gulp_min_css = require("gulp-minify-css");
var gulp_sass = require("gulp-sass");
var gulp_plumber = require("gulp-plumber");
var gulp_minimg = require("gulp-tinypng");
var gulp_uglify = require("gulp-uglify");
var gulp_copy = require("gulp-copy");
var gulp_autoprefixer = require("gulp-autoprefixer");

//复制.html
gulp.task("copy",function(){
	gulp.src('./src/*.html')
		.pipe(gulp_copy('./dist',{prefix:"1"}));
	gulp.src('./src/font/**/*')
		.pipe(gulp_copy('./dist/font/',{prefix:"2"}));
	gulp.src('./src/media/**/*')
		.pipe(gulp_copy('./dist/media/',{prefix:"2"}));
})
//定义任务
gulp.task("mincss",function(){
	//这个任务具体要做的事情就写在这里
	gulp.src('./src/scss/**/*.scss')//source源
		.pipe(gulp_plumber())
		.pipe(gulp_sass())
		.pipe(gulp_min_css())
		.pipe(gulp_autoprefixer({
				browsers:['last 2 versions','Android >= 4.0','IOS 7','last 3 Safari versions'],
				cascade:false
			}))
		.pipe(gulp_min_css({
			keepSpecialComments: '*'
		}))
		.pipe(gulp.dest('./dist/css'));
	
})
//压缩图片
gulp.task("minimg",function(){
	gulp.src('./src/images/**/*.*')
		.pipe(gulp_minimg("rKMyV7Q_V8TX37IbqbeQfeAW3e6AxM18"))
		.pipe(gulp.dest('./dist/images'));
})
//实时监听sass文件，并转换
gulp.task("watchscss",function(){
	gulp.watch(
		"./scss/**/*.scss",["mincss"]
	)
})
//压缩JS
gulp.task("minjs",function(){
	gulp.src('./src/js/**/*.js')
		.pipe(gulp_uglify())
		.pipe(gulp.dest('./dist/js'));
})
//定义自动刷新浏览器任务
var browser_sync= require("browser-sync").create();
gulp.task('browser-sync', function() {
    browser_sync.init({
    	port:Math.floor(Math.random()*9999)+1000,
        server: {
            baseDir: "./src"
        }
    });
    	gulp.watch("scss/**/*.scss",["mincss"]).on("change",browser_sync.reload)
    gulp.watch("*.html").on("change",browser_sync.reload);
});

//一键开发模式
gulp.task("dev",["browser-sync","copy"]);

//一键生成模式
gulp.task("pro",["mincss","minjs"]);
